class BoundedQueue

A very simple bounded double-ended queue/stack used for caching a list of values. Overrides a few methods from Array so that if more than a certain number of elements are added, entries at the back of the array will be popped off

Public Class Methods

new(max_size) click to toggle source

Initialize a queue with a set length. The backing array is not resized to match this size.

Calls superclass method
# File lib/bounded_queue.rb, line 15
def initialize(max_size)
  super()
  @max_size = max_size
end

Public Instance Methods

push(element) click to toggle source

Pushes an element onto the front of the “queue”, removing the last element in the queue if necessary.

# File lib/bounded_queue.rb, line 23
def push(element)
  pop if size + 1 >= @max_size
  push_internal(element)
end
Also aliased as: push_internal
push_internal(element)
Alias for: push
unshift(element) click to toggle source

Pushes an element onto the front of the “stack”, removing the first element in the stack if necessary.

# File lib/bounded_queue.rb, line 31
def unshift(element)
  shift if size + 1 >= @max_size
  unshift_internal(element)
end
Also aliased as: unshift_internal
unshift_internal(element)
Alias for: unshift